﻿#include "UdpSocket.h"

#ifdef _WIN32 

bool UdpSocket::Create(const Desc& desc)
{
    WORD wVersionRequested;
    WSADATA wsaData;
    int err;

    wVersionRequested = MAKEWORD(2, 2);

    err = WSAStartup(wVersionRequested, &wsaData);
    if (err != 0) {
        LOGI("WSAStartup failed with error: ");
        return 0;
    }


    sock = socket(desc.af, desc.type, desc.protocol);

    if (INVALID_SOCKET == sock)
    {
        //LOGI("socket error" , WSAGetLastError() );
        LOGI("socket error");
        WSACleanup();
        return 0;
    }

    LOGI("socket create successfully");
    return 1;
}

void UdpSocket::Close()
{
    closesocket(sock);
    WSACleanup();
}

#endif // _WIN32


#ifdef __linux__
bool UdpSocket::Create(const Desc &desc)
{

    sock = socket(desc.af, desc.type, desc.protocol);

    if (-1 == sock)
    {
        LOGI("socket error", "");
        return false;
    }

    LOGI("socket create successfully");
    return true;
}

void UdpSocket::Close()
{
    close(sock);
}
#endif





